package com.beidouin.sensortest;

import com.friendlyarm.AndroidSDK.HardwareControler;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.renderscript.Byte2;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import com.friendlyarm.AndroidSDK.HardwareControler;
import com.friendlyarm.Utils.CommonFuncs;

public class PWMActivity extends AppCompatActivity implements View.OnClickListener {
    EditText textEditor;
    Button btnSub;
    Button btnAdd;
    Button playbtn;
    Button stopbtn;
    HardwareControler hw;

    final int smart4418PWMGPIOPin = 77;//97; //GPIOD1, Smart4418/NanoPC-T2/NanoPC-T3

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwm);
        btnSub = ((Button) findViewById(R.id.btnSub));
        btnAdd = ((Button) findViewById(R.id.btnAdd));
        playbtn= (Button) findViewById(R.id.playbtn);
        stopbtn= (Button) findViewById(R.id.stopbtn);

        btnSub.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        playbtn.setOnClickListener(this);
        stopbtn.setOnClickListener(this);

        textEditor = ((EditText) findViewById(R.id.editText));
        hw = new HardwareControler();

        textEditor.setText("" + 500);
        stopPWM();
    }

    private void playPWM() {
        int f;
        f = Integer.valueOf(textEditor.getText().toString()).intValue();
        if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) {
            hw.PWMPlayEx(smart4418PWMGPIOPin, f);
        }
    }

    private void stopPWM() {
        if (HardwareControler.isS5P4418Board() || HardwareControler.isS5P6818Board()) {
            hw.PWMStopEx(smart4418PWMGPIOPin);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            stopPWM();
        }
        return super.onKeyDown(keyCode,event);
    }
    public void onClick(View v) {
        int f;
        switch (v.getId()) {
            case R.id.btnSub:
                f = Integer.valueOf(textEditor.getText().toString()).intValue() - 100;
                if (f < 1) {
                    f = 1;
                }
                textEditor.setText("" + f);
                playPWM();
                break;
            case R.id.btnAdd:
                f = Integer.valueOf(textEditor.getText().toString()).intValue() + 100;
                if (f < 1) {
                    f = 1;
                }
                textEditor.setText("" + f);
                playPWM();
                break;
            case R.id.playbtn:
            {
                playPWM();
            }
           break;
            case R.id.stopbtn:
            {
                stopPWM();
            }
            default:
                break;
        }
    }
}
